package com.asen.libcommon.util

import android.annotation.SuppressLint
import android.view.View
import com.jakewharton.rxbinding2.view.RxView
import com.lxj.xpopup.core.BasePopupView
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * @author Lin
 * @time 2020/12/4 14:51
 *
 * @desc Rx工具类
 */

object RxUtils {

    /**
     * 加载框
     */
    fun <T> loadingTransformer(popupView: BasePopupView,isShow:Boolean): ObservableTransformer<T, T> {
        return ObservableTransformer<T,T>{upstream->
            upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe {
                    popupView.show()
                }.doOnTerminate {
                    popupView.dismiss()
                }
        }
    }

    /**
     * 防抖
     */
    @SuppressLint("CheckResult")
    inline fun View.click(time:Long = 1,crossinline call:((View)->Unit))=
        RxView.clicks(this)
            .throttleFirst(time, TimeUnit.SECONDS)
            .subscribe{
                call.invoke(this)
            }


}